package com.tools.cleanmaster.service;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;

import com.tools.cleanmaster.R;


/**
 * CreatedXXX on 16/1/4.
 */
public class StickService extends Service {

    public static final String TAG = "fan";

    public static final int FORGROUND_SERVICE_ID = 100086;

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Intent intent = new Intent();
            intent.setClassName(StickService.this, "com.antivirus.core.ui.activity.MainActivity");
            PendingIntent pi = PendingIntent.getActivity(StickService.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            Notification notification = new NotificationCompat.Builder(StickService.this)
                    .setSmallIcon(R.drawable.transparent)
                    .setContentIntent(pi)
                    .build();
            startForeground(FORGROUND_SERVICE_ID, notification);
            stopService(new Intent(StickService.this, StickInnerService.class));
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        if (Build.VERSION.SDK_INT >= 18) {
            startService(new Intent(this, StickInnerService.class));
            mHandler.sendEmptyMessageDelayed(0, 20);
        } else {
            Notification notification = new Notification();
            startForeground(StickService.FORGROUND_SERVICE_ID, notification);
        }
    }

    @Override
    public void onDestroy() {
        stopForeground(true);
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
    }

}
